package com.mlink.ai.chat.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.mlink.ai.chat.assistant.robot.R
import com.mlink.ai.chat.utils.ParentCategory

class TopicPageAdapter(var context: Context, var list: List<ParentCategory>) : PagerAdapter() {

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = LayoutInflater.from(context).inflate(R.layout.item_parent_topic, null)
        val rvContent = view.findViewById<RecyclerView>(R.id.rv_child_topic)
        initAdapter(rvContent, list[position])
        container.addView(view)
        return view
    }

    override fun getCount(): Int {
        return list.size
    }

    override fun isViewFromObject(view: View, any: Any): Boolean {
        return view == any
    }

    override fun destroyItem(container: ViewGroup, position: Int, view: Any) {
        container.removeView(view as View)
    }


    fun initAdapter(rvContent: RecyclerView, parentCategory: ParentCategory) {
        val adapter = ChildAdapter(parentCategory.contents)
        rvContent.adapter = adapter
        rvContent.layoutManager = LinearLayoutManager(context)
    }

    override fun getPageTitle(position: Int): CharSequence {
        return list[position].title
    }
}